package cn.blogger.controller;

import cn.blogger.pojo.TTag;
import cn.blogger.service.TagService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.blogger.entity.Result;

import javax.ws.rs.PUT;
import java.util.List;

@RestController
@RequestMapping("/tag")
@CrossOrigin
public class TagController {
    @Reference
    private TagService tagService;

    //新增
    @PostMapping("/add.do")
    public Result add(@RequestBody TTag tTag){
        try {
            tagService.insert(tTag);
        }catch (Exception e){
            return new Result(false, "新增标签失败");
        }
        return new Result(false, "新增标签成功");
    }

    //查询所有标签
    @GetMapping("/selectAll.do")
    public List<TTag> all(@RequestParam(value = "currentPage") Integer currentPage,
                          @RequestParam(value = "pageSize") Integer pageSize){
        return tagService.listAll(currentPage, pageSize);
    }

    //更新标签
    @PutMapping ("/update.do")
    public Result update(@RequestBody TTag tTag){
        try {
            tagService.update(tTag);
        }catch (Exception e){
            return new Result(false,"标签更新失败");
        }
        return new Result(true,"标签更新成功");
    }
    // 删除标签
    @DeleteMapping ("/delete.do")
    public Result delete(@RequestParam(value="uid")Integer uid){
        try {
            tagService.deleteById(uid);
        }catch (Exception e){
            return new Result(false,"标签删除失败");
        }
        return new Result(true,"标签删除成功");
    }
}